#!/usr/local/bin/python
# __    _____ _____ _____    ___     ___ 
#|  |  |  _  |     |   __|  |_  |   |   |
#|  |__|     | | | |__   |   _| |_ _| | |
#|_____|__|__|_|_|_|_____|  |_____|_|___|

# 
# Author: Carl Javier 4088697
# Date: 22 September 2008 rev 1.0
# Date: 24 September 2008 rev 1.1

# Script to gather information about
# sip cilents connected or not connected
# IP address, extensions


import sys,os,string


TELNET="/usr/bin/telnet"
GREP="/usr/bin/grep"

SCRIPTPATH=str(sys.path[0]) + "\/"
CUT="/usr/bin/cut"
SH="/bin/sh"
GMETRIC="/usr/local/bin/gmetric"
HOSTNAME="PBX1"

sip_peers_cmd=SH + " "  + SCRIPTPATH+ "asterisk_sipshowpeers.sh | " + TELNET + " | " + GREP + " '[0-9]' |" + GREP + " -v Asterisk | "+GREP + " -v Trying |" + GREP + " -v '^$' |" + GREP + " -v Monitored"

#print sip_peers_cmd
sipHostList = os.popen(sip_peers_cmd,'r')


sippeers=[]
#print "\nSIP SHOW PEERS \n"
#Obtain List of SIP PEERS from CLI>sip show peers 
for x in sipHostList:
	#print x[:-1]
	sippeers.append(string.split(x[:-1]))
	#print sippeer

sipHostList.close()


sipchannels=[]

#Obtain which channels are being used
#sip_channel_cmd=SH + " "  + SCRIPTPATH+ "asterisk_sipshowchannels.sh | " + TELNET + " | " + GREP + " '[0-9]' |" + GREP + " -v Asterisk | "+GREP + " -v Trying |" + GREP + " -v '^$' |" + GREP + " -v SIP"
#sipChannelList = os.popen(sip_channel_cmd,'r')

#print "\nSIP SHOW Channels \n"
#for a in sipChannelList:
	#print a[:-1]
	##sipchannels.append(string.split(a[:-1]))

#sipChannelList.close()


#Obtain which channels are being used
show_channel_cmd=SH + " "  + SCRIPTPATH+ "asterisk_showchannels.sh | " + TELNET + " | " + GREP + " Up"

showChannelList = os.popen(show_channel_cmd,'r')

#print "\nShow Channels \n" 
for a in showChannelList:
	#print a[:-1]
	sipchannels.append(string.split(a[:-1]))

showChannelList.close()


#print sippeers


#print sipchannels

#for x in sipchannels:
		#channel=str(string.split(string.split(x[0],'/')[1],'-')[0])
		
		#print "Phone: " + channel + " calling: "# + calling

# Map which SIP Clients are in a Call or not and inject their stats
# into the Ganglia system using GMETRIC
#print sipchannels
#print "\n"+str(len(sipchannels))
for i in sippeers:
	#print i
	
	username=str(string.split(i[0],'/')[0])
	IP=str(i[1])
	onhook="yes"
	calling="no one"
	channel="none"
	#print username
	#print IP
	
	for x in sipchannels:
		channelusername=str(string.split(string.split(x[0],'/')[1],'-')[0])
		
		#print "Phone: " + channelusername + " calling: " + calling
		#print x[0]
		channel=str(x[0])
		
		if (channelusername==username):
			onhook="no"
			calling=str(string.split(x[1],'@')[0])
	
	if (IP=="(Unspecified)"):
		IP="Unknown"
	if (calling=="(None)"):
		calling="receiving"
		
		
	#print username +" IP address:"+IP +" In call:" + incall	+ " Calling:" + calling
	
	gm_cmd=GMETRIC+" --name "+ HOSTNAME+"-Client:"+username
	
	#print gm_cmd +"-IP --value " + IP + " --type string --units IP"
	#print gm_cmd +"-OnHook --value " + onhook + " --type string --units onhook"
	#print gm_cmd +"-Dialed --value " + calling + " --type string --units extension"
	os.system(gm_cmd +"-SipPhoneIP --value " + IP + " --type string --units IP")
	os.system(gm_cmd +"-OnHook --value " + onhook + " --type string --units onhook")
	os.system(gm_cmd +"-Dialed --value " + calling + " --type string --units extension")
	os.system(gm_cmd +"-Channel --value " + channel + " --type string")
